package com.goblin.config;

import com.goblin.security.RequestWrapperFilter;
import com.goblin.security.SecurityFilter;
import org.apache.catalina.filters.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class FilterConfiguration {

	//@Bean
	//public CorsFilter corsFilter() {
	//	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	//
	//	CorsConfiguration corsConfiguration = new CorsConfiguration();
	//	corsConfiguration.addAllowedOrigin("*");
	//	corsConfiguration.addAllowedHeader("*");
	//	corsConfiguration.addAllowedMethod("*");
	//
	//	source.registerCorsConfiguration("/**", corsConfiguration);
	//	return new CorsFilter();
	//}

	@Bean
	public FilterRegistrationBean requestWrapperFilter() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new RequestWrapperFilter());
		registration.addUrlPatterns("/*");
		registration.setOrder(2);
		return registration;
	}

	@Bean
	public FilterRegistrationBean securityFilter() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new SecurityFilter());
		registration.addUrlPatterns("/*");
		registration.setOrder(3);
		return registration;
	}

}
